<?php

namespace App\Models;

use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;

/*音频资源管理model*/

class ResourceAudioInfo extends BaseModel
{
    use HasFactory;

    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'change_time';


    protected $table = 'resource_audio_info';


    /*关联活动类型*/
    public function conType()
    {
        return $this->hasOne(ResourceToTalType::class, 'id', 'type_id');
    }

    /**
     * 获取音频资源列表
     * @param keywords 检索条件
     * @param type_id 类型id
     * @param is_play 是否显示  1 显示  2 隐藏
     * @param page int 当前页
     * @param limit int 分页大小
     */
    public function lists($search_data = [])
    {
        $type_id = $search_data['type_id'] ?? '';
        $is_play = $search_data['is_play'] ?? '';
        $keywords = $search_data['keywords'] ?? '';
        $limit = !empty($search_data['limit']) ? $search_data['limit'] : 10;

        $field = ['id', 'guid', 'title', 'author', 'type_id', 'img', 'size', 'press', 'pre_time','intro', 'browse_num', 'create_time', 'is_play'];
        $res = $this->select($field)
            ->with('conType', function ($query) {
                $query->select('id', 'type_name')->where('is_del', 1);
            })
            ->where(function ($query) use ($keywords) {
                if ($keywords) {
                    $query->Orwhere('title', 'like', "%$keywords%")->Orwhere('author', 'like', "%$keywords%");
                }
            })->where(function ($query) use ($type_id, $is_play) {
                if ($type_id) {
                    $query->where('type_id', $type_id);
                }
                if ($is_play) {
                    $query->where('is_play', $is_play);
                }
            })->where('is_del', 1)
            ->orderByDesc('id')
            ->paginate($limit)
            ->toArray();

        return $res;
    }

    /**
     * 获取音频资源详情
     * @param guid 音频id
     * @param is_play 是否显示  1 显示  2 隐藏
     */
    public function detail($id, $guid = null, $is_play = null)
    {
        if (empty($id) && empty($guid)) {
            return false;
        }
        $field = ['id', 'guid', 'title', 'author', 'type_id', 'img', 'size', 'press', 'pre_time', 'intro', 'browse_num', 'is_play', 'create_time'];
        $res = $this->select($field)
            ->with('conType', function ($query) {
                $query->select('id', 'type_name')->where('is_del', 1);
            })->where(function ($query) use ($id, $guid, $is_play) {
                if ($id) {
                    $query->where('id', $id);
                } else {
                    $query->where('guid', $guid);
                }
                if ($is_play) {
                    $query->where('is_play', $is_play);
                }
            })->where('is_del', 1)
            ->first();

        return $res;
    }

    /**
     *添加资源
     * @param $data
     */
    public function addResource($data)
    {
        $data['guid'] = get_guid();
        $this->add($data);

        //资源添加到总数据库
        $resourceTotalModel = new ResourceTotal();
        $resourceTotalModel->add([
            'guid' => $data['guid'],
            'title' => $data['title'],
            'author' => $data['author'],
            'press' => $data['press'],
            'pre_time' => $data['pre_time'],
            'img' => $data['img'],
            'intro' => $data['intro'],
            'type_id' => $data['type_id'],
            'is_play' => 1,
            'big_type_id' => 2,
        ]);
        return true;
    }
    /**
     * 修改资源
     */
    public function changeResource($data)
    {
        $this->where('guid', $data['guid'])->update([
            'title' => $data['title'],
            'author' => $data['author'],
           // 'isbn' => $data['isbn'],
            'press' => $data['press'],
            'pre_time' => $data['pre_time'],
            'img' => $data['img'],
            'intro' => $data['intro'],
            'type_id' => $data['type_id'],
            'change_time' => date('Y-m-d H:i:s'),
        ]);
        //资源添加到总数据库
        $resourceTotalModel = new ResourceTotal();
        $resourceTotalModel->where('guid', $data['guid'])->update([
            'title' => $data['title'],
            'author' => $data['author'],
          //  'isbn' => $data['isbn'],
            'press' => $data['press'],
            'pre_time' => $data['pre_time'],
            'img' => $data['img'],
            'intro' => $data['intro'],
            'type_id' => $data['type_id'],
            'change_time' => date('Y-m-d H:i:s'),
        ]);
        return true;
    }

    /**
     * 隐藏显示资源
     */
    public function playResource($guids, $is_play)
    {
        $guids_arr = explode(',', $guids);
        foreach ($guids_arr as $key => $val) {
            $this->where('guid', $val)->update([
                'is_play' => $is_play,
                'change_time' => date('Y-m-d H:i:s'),
            ]);
            //资源添加到总数据库
            $resourceTotalModel = new ResourceTotal();
            $resourceTotalModel->where('guid', $val)->update([
                'is_play' => $is_play,
                'change_time' => date('Y-m-d H:i:s'),
            ]);
        }
        return true;
    }

    /**
     * 删除资源
     */
    public function delResource($guid)
    {
        $this->where('guid', $guid)->update([
            'is_del' => 2,
            'change_time' => date('Y-m-d H:i:s'),
        ]);
        //资源添加到总数据库
        $resourceTotalModel = new ResourceTotal();
        $resourceTotalModel->where('guid', $guid)->update([
            'is_del' => 2,
            'change_time' => date('Y-m-d H:i:s'),
        ]);
        return true;
    }

    /**
     * 更新资源量大小
     *@param audio_id 音频id
     */
    public function updateResourceSize($audio_id)
    {
        $size = ResourceAudioChapterInfo::where('audio_id', $audio_id)->where('is_del', 1)->sum('size');
        $this->where('id', $audio_id)->update([
            'size' => $size,
            'change_time' => date('Y-m-d H:i:s'),
        ]);
    }
}
